import corebrowserutil
import uix
import uiutil
import util
import urlparse
import service
import uicls
import uiconst

def DefaultHomepage():
    hp = 'http://www.eveonline.com/mb2/news.asp'
    if (boot.region == 'optic'):
        hp = 'http://eve.gtgame.com.cn/gamenews/index.htm'
    if (eve.session.role & service.ROLE_PLAYER):
        newhp = sm.GetService('sites').GetDefaultHomePage()
        if (newhp is not None):
            if (newhp != ''):
                hp = newhp
        return hp



def GetFixedURL(parent, url):
    browser = uiutil.GetBrowser(parent)
    currentURL = None
    if (browser and hasattr(browser.sr, 'currentURL')):
        currentURL = browser.sr.currentURL
    return urlparse.urljoin(currentURL, url)



def SanitizedTypeID(typeID):
    try:
        typeID = int(typeID)
    except:
        uix.LogError('Unable to convert typeID into an integer!')
        return 
    if (typeID is None):
        return 
    else:
        typeObj = cfg.invtypes.GetIfExists(typeID)
        if (typeObj is None):
            return 
        return typeID



def SanitizedItemID(itemID):
    try:
        itemID = int(itemID)
    except:
        uix.LogError('Unable to convert itemID into an integer!')
        return 
    return itemID



def SanitizedSolarsystemID(solarsystemID):
    try:
        solarsystemID = int(solarsystemID)
    except:
        uix.LogError('Unable to convert solarsystemID into an integer!')
        return 
    if not util.IsSolarSystem(solarsystemID):
        return 
    else:
        return solarsystemID


exports = {'browserutil.DefaultHomepage': DefaultHomepage,
 'browserutil.GetFixedURL': GetFixedURL,
 'browserutil.SanitizedTypeID': SanitizedTypeID,
 'browserutil.SanitizedItemID': SanitizedItemID,
 'browserutil.SanitizedSolarsystemID': SanitizedSolarsystemID}

